//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp.view;

import org.prx.prp.R;
import org.prx.prp.controller.ProgramManager;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class FavoritesOnDemandActivity extends ListActivity {
	private Context mContext;
	Cursor mCursor;
	SimpleCursorAdapter adapter;
	private Handler handler = new Handler() {
		public void handleMessage(Message g) {
			mCursor.requery();
			adapter.notifyDataSetChanged();
		}
	};

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info;
		try {
			info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		} catch (ClassCastException e) {
			return false;
		}
		Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
		int programId = cursor.getInt(cursor.getColumnIndex("PROGRAM_ID"));
		ProgramManager.removeFavorite(programId);
		handler.sendEmptyMessage(1);
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View view,
			ContextMenuInfo menuInfo) {
		menu.add("Remove Favorite");
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			getListView().setHorizontalFadingEdgeEnabled(false);
			getListView().setBackgroundColor(Color.parseColor("#FFFFFF"));
			getListView().setCacheColorHint(Color.parseColor("#FFFFFF"));
			registerForContextMenu(getListView());
			mContext = this;
			mCursor = ProgramManager.findAllFavorites();
			startManagingCursor(mCursor);
			adapter = new SimpleCursorAdapter(this,
					R.layout.list_single_item_view, mCursor,
					new String[] { "PROGRAM_TITLE" }, new int[] { R.id.text1 });
			ListView listView = getListView();
			setContentView(listView);
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Cursor cursor = (Cursor) adapter.getItem(position);
					int programIDOfSelectedItem = cursor.getInt(cursor
							.getColumnIndex("PROGRAM_ID"));
					Bundle bundle = new Bundle();
					bundle.putInt("PROGRAM_ID", programIDOfSelectedItem);
					Intent intent = new Intent(mContext, ProgramInfoView.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			});

			setListAdapter(adapter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
